package q841_canVisitAllRooms;

import java.util.List;

public class Solution_1 {
    boolean[] vis;
    /*
    经典dfs 一开始从0号房间进入 然后依次dfs所有有钥匙且没有被进入过的房间
     */
    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        vis = new boolean[rooms.size()];
        dfs(rooms, 0);

        for (boolean b : vis) {
            if (!b) return false;
        }
        return true;
    }

    private void dfs(List<List<Integer>> rooms, int i) {
        vis[i] = true;
        for (Integer room : rooms.get(i)) {
            if (!vis[room]) dfs(rooms, room);
        }
    }
}
